package com.example.scenictourback.controller;

import com.example.scenictourback.common.Result;
import com.example.scenictourback.service.OrderService;
import com.example.scenictourback.service.ScenicAreaService;
import com.example.scenictourback.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/stats")
@PreAuthorize("hasRole('ADMIN')")
public class StatisticsController {
    
    @Autowired
    private OrderService orderService;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private ScenicAreaService scenicAreaService;
    
    /**
     * 获取系统总体统计数据
     */
    @GetMapping("/dashboard")
    public Result<?> getDashboardStats() {
        Map<String, Object> stats = new HashMap<>();
        
        // 订单统计
        stats.put("orders", orderService.getOrderStats());
        
        // 用户统计
        stats.put("users", userService.getUserStats());
        
        // 景区统计
        Map<String, Object> scenicStats = new HashMap<>();
        scenicStats.put("totalScenicAreas", scenicAreaService.countScenicAreas());
        scenicStats.put("openScenicAreas", scenicAreaService.countOpenScenicAreas());
        stats.put("scenicAreas", scenicStats);
        
        return Result.success(stats);
    }
    
    /**
     * 获取详细订单统计数据
     */
    @GetMapping("/orders")
    public Result<?> getOrderStats() {
        return Result.success(orderService.getOrderStats());
    }
    
    /**
     * 获取详细用户统计数据
     */
    @GetMapping("/users")
    public Result<?> getUserStats() {
        return Result.success(userService.getUserStats());
    }
} 